package pri.ydy.eduService.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import pri.ydy.commonUtils.R;
import pri.ydy.eduService.entity.EduChapter;
import pri.ydy.eduService.entity.chapter.ChapterVo;
import pri.ydy.eduService.service.EduChapterService;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-10-06
 */
@RestController
@RequestMapping("/eduservice/edu-chapter")
public class EduChapterController {

    @Autowired
    private EduChapterService chapterService;

    //课程列表大纲，根据课程id进行查询
    @GetMapping("getCharterVideo/{courseId}")
    public R getCharterVideo(@PathVariable String  courseId){
        List<ChapterVo> list=chapterService.getChapterVideoByCourseId(courseId);
        return R.ok().data("allChapterVideo",list);
    }

    //添加章节
    @PostMapping("addChapter")
    public R addChapter(@RequestBody EduChapter chapter){
        boolean save = chapterService.save(chapter);
        if(save) return R.ok();
        else return R.error();
    }

    //根据id查询章节
    @GetMapping("getChapterInfo/{chapterId}")
    public R getChapterInfo(@PathVariable String chapterId){
        EduChapter eduChapter = chapterService.getById(chapterId);
        return R.ok().data("chapter",eduChapter);
    }

    //修改章节
    @PostMapping("updateChapterInfo")
    public R updateChapterInfo(@RequestBody EduChapter eduChapter){
        chapterService.updateById(eduChapter);
        return R.ok();
    }

    //删除章节
    @DeleteMapping("{chapterId}")
    public R deleteChapterById(@PathVariable String  chapterId) {
        boolean flag = chapterService.deleteChapter(chapterId);
        if (flag) {
            return R.ok();
        }else{
            return R.error();
        }
    }

}

